package vip.liux.front.application.clients;

import com.fasterxml.jackson.annotation.JsonFormat;
import feign.Logger;
import feign.codec.Encoder;
import feign.form.FormEncoder;
import feign.form.spring.SpringFormEncoder;
import lombok.Data;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

@FeignClient(name = "openApi", url = "https://hot.cigh.cn",
        configuration = OpenApiFeignClient.OpenApiClientFeignConfiguration.class,
        fallbackFactory = OpenApiFeignClient.OpenApiFeignClientFallbackFactory.class
)
public interface OpenApiFeignClient {

    /**
     * 获取bilibili 热搜
     *
     * @return ResponseData
     */
    @GetMapping("/bilibili")
    ResponseData<BilibiliDto> bilibili();

    /**
     * 微博热搜
     *
     * @return ResponseData
     */
    @GetMapping("/weibo")
    ResponseData<WeiboDto> weibo();

    /**
     * 知乎热搜
     *
     * @return ResponseData
     */
    @GetMapping("/zhihu")
    ResponseData<ZhihuDto> zhihu();

    /**
     * 百度热搜
     *
     * @return ResponseData
     */
    @GetMapping("/baidu")
    ResponseData<BaiduDto> baidu();

    /**
     * 抖音-热点榜
     *
     * @return ResponseData
     */
    @GetMapping("/douyin_new")
    ResponseData<Object> douyinNew();

    /**
     * 抖音-热歌榜
     *
     * @return ResponseData
     */
    @GetMapping("/douyin_music")
    ResponseData<Object> douyinMusic();

    /**
     * 豆瓣-新片榜
     *
     * @return ResponseData
     */
    @GetMapping("/douban_new")
    ResponseData<Object> doubanNew();

    /**
     * 豆瓣-讨论榜
     *
     * @return ResponseData
     */
    @GetMapping("/douban_group")
    ResponseData<Object> doubanGroup();

    /**
     * 百度贴吧
     *
     * @return ResponseData
     */
    @GetMapping("/tieba")
    ResponseData<Object> tieba();

    /**
     * 少数派
     *
     * @return ResponseData
     */
    @GetMapping("/sspai")
    ResponseData<Object> sspai();

    /**
     * 澎湃新闻
     *
     * @return ResponseData
     */
    @GetMapping("/thepaper")
    ResponseData<Object> thepaper();

    /**
     * 今日头条
     *
     * @return ResponseData
     */
    @GetMapping("/toutiao")
    ResponseData<Object> toutiao();

    /**
     * 36 氪
     *
     * @return ResponseData
     */
    @GetMapping("/36kr")
    ResponseData<Object> kr36();

    /**
     * 稀土掘金
     *
     * @return ResponseData
     */
    @GetMapping("/juejin")
    ResponseData<Object> juejin();

    /**
     * 腾讯新闻
     *
     * @return ResponseData
     */
    @GetMapping("/newsqq")
    ResponseData<Object> newsqq();

    /**
     * 网易新闻
     *
     * @return ResponseData
     */
    @GetMapping("/netease")
    ResponseData<Object> netease();

    /**
     * 英雄联盟
     *
     * @return ResponseData
     */
    @GetMapping("/lol")
    ResponseData<Object> lol();

    /**
     * 原神
     *
     * @return ResponseData
     */
    @GetMapping("/genshin")
    ResponseData<Object> genshin();

    /**
     * 微信读书
     *
     * @return ResponseData
     */
    @GetMapping("/weread")
    ResponseData<Object> weread();

    /**
     * 快手
     *
     * @return ResponseData
     */
    @GetMapping("/kuaishou")
    ResponseData<Object> kuaishou();

    /**
     * 网易云音乐
     *
     * @return ResponseData
     */
    @GetMapping("/netease_music_toplist")
    ResponseData<Object> neteaseMusicTopList();

    /**
     * QQ音乐
     *
     * @return ResponseData
     */
    @GetMapping("/qq_music_toplist")
    ResponseData<Object> qqMusicToplist();

    /**
     * NGA
     *
     * @return ResponseData
     */
    @GetMapping("/ngabbs")
    ResponseData<Object> nga();

    /**
     * Github
     *
     * @return ResponseData
     */
    @GetMapping("/github")
    ResponseData<Object> github();

    class OpenApiFeignClientFallbackFactory implements FallbackFactory<OpenApiFeignClient> {
        private static final org.slf4j.Logger logger = getLogger(OpenApiFeignClientFallbackFactory.class);

        @Override
        public OpenApiFeignClient create(Throwable throwable) {
            logger.error("异常原因:{}", throwable.getMessage(), throwable);
            return new OpenApiFeignClient() {
                public ResponseData<BilibiliDto> bilibili() {
                    return null;
                }

                public ResponseData<WeiboDto> weibo() {
                    return null;
                }

                public ResponseData<ZhihuDto> zhihu() {
                    return null;
                }

                public ResponseData<BaiduDto> baidu() {
                    return null;
                }

                public ResponseData<Object> douyinNew() {
                    return null;
                }

                public ResponseData<Object> douyinMusic() {
                    return null;
                }

                public ResponseData<Object> doubanNew() {
                    return null;
                }

                public ResponseData<Object> doubanGroup() {
                    return null;
                }

                public ResponseData<Object> tieba() {
                    return null;
                }

                public ResponseData<Object> sspai() {
                    return null;
                }

                public ResponseData<Object> thepaper() {
                    return null;
                }

                public ResponseData<Object> toutiao() {
                    return null;
                }

                public ResponseData<Object> kr36() {
                    return null;
                }

                public ResponseData<Object> juejin() {
                    return null;
                }

                public ResponseData<Object> newsqq() {
                    return null;
                }

                public ResponseData<Object> netease() {
                    return null;
                }

                public ResponseData<Object> lol() {
                    return null;
                }

                public ResponseData<Object> genshin() {
                    return null;
                }

                public ResponseData<Object> weread() {
                    return null;
                }

                public ResponseData<Object> kuaishou() {
                    return null;
                }

                public ResponseData<Object> neteaseMusicTopList() {
                    return null;
                }

                public ResponseData<Object> qqMusicToplist() {
                    return null;
                }

                public ResponseData<Object> nga() {
                    return null;
                }

                public ResponseData<Object> github() {
                    return null;
                }
            };
        }
    }

    @Data
    class ResponseData<R> {
        private long code;
        private String message;
        private String name;
        private String title;
        private String subtitle;
        private String from;
        private long total;
        @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
        private LocalDateTime updateTime;
        private List<R> data;
    }

    @Data
    class BilibiliDto {
        private String id;
        private String title;
        private String desc;
        private String pic;
        private Object data;
        private Object owner;
        private long hot;
        private String url;
        private String mobileUrl;
    }

    @Data
    class WeiboDto {
        private String title;
        private String desc;
        private String url;
        private String mobileUrl;
    }

    @Data
    class ZhihuDto {
        private String title;
        private String desc;
        private String pic;
        private long hot;
        private String url;
        private String mobileUrl;
    }

    @Data
    class BaiduDto {
        private String title;
        private String desc;
        private String pic;
        private long hot;
        private String url;
        private String mobileUrl;
    }

    class OpenApiClientFeignConfiguration {
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.BASIC;
        }

        @Bean
        public Encoder feignEncoder() {
            return new FormEncoder(new SpringFormEncoder());
        }
        @Bean
        public OpenApiFeignClientFallbackFactory openApiFeignClientFallbackFactory() {
            return new OpenApiFeignClientFallbackFactory();
        }
    }
}
